package hn.cch.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

    private static Logger logger = LoggerFactory.getLogger(DateUtil.class);

    /**
     * 根据String的时间格式将其转化为Date
     *
     * @param pattern
     * @param text
     * @return
     */
    public static Date toDate(String pattern, String text) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            Date date = sdf.parse(text);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
            logger.error("date format parse error : " + e.getMessage());
            return null;
        }

    }

    /**
     * 将Date转化为指定时间格式的String
     *
     * @param pattern
     * @param date
     * @return
     */
    public static String toText(String pattern, Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String text = sdf.format(date);
        return text;
    }




}
